<?php
/**
 * Created by PhpStorm.
 * User: zhenghao
 * Date: 2015/6/19
 * Time: 10:12
 */

/**
 * Class ProductBO
 * 产品模块业务逻辑处理
 */
class ProductBO {

    //获取指定币种与人民币的最新汇率
    static function getRateById($id){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."price_rate";

        $str=array(
            "fields" => "product_id,buy,renew_date",
            "where" => "product_id=$id and `disable`=0",
            "order" => "renew_date DESC",
            "count" => 1
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        if(empty($result)) throw new Exception("not found any row for id:$id");

        return $result[0];

    }


    //获取指定品类期货价格比值，最后30天内数据
    static function getLastSpecificValueForFuture($type){

        //将type转换为小写 （数据库里是小写保存）
        $type=strtolower($type);

        global $conf;

        //获取指定类型，最后记录时间
        $last_date=self::getMaxDateForSpecificValue($type);

        //按30天间隔，计算查询开始时间，结束时间
        $begin_date=date("Y-m-d",strtotime("$last_date -30 days"));

        //按时间区间执行查询，返回数据
        $table_name=$conf["DB_PREFIX"]."shfe";

        $str=array(
            "fields" => "shfe,product_type,addtime",
            "where" => "product_type='".$type."' and addtime BETWEEN '".$begin_date."' and '".$last_date."'",
            "order" => "addtime"
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        return $result;
    }

    //获取指定类型期货比值数据记录最大时间
    private static function getMaxDateForSpecificValue($type){

        global $conf;

        $table_name=$conf["DB_PREFIX"]."shfe";

        $str=array(
            "fields" => "MAX(addtime) as addtime",
            "where" => "product_type='".$type."'"
        );

        $db=new MyAdodb();

        $result=$db->select($table_name,$str);

        if(empty($result)) throw new Exception("not found any row for type:$type");

        return $result[0]["addtime"];
    }

    //获取主产品中价格当日未更新的产品价格详情
    static function mainPriceNotUpdate($pid_array){

        //获取当前日期
        $now_date=date('Y-m-d');

        global $conf;

        $db=new MyAdodb();

        //处理产品编号
        $str_pid=implode(",",$pid_array);

        $table_name=$conf["DB_PREFIX"]."price_product_latest as pcl ,".$conf["DB_PREFIX"]."product_detail as pd";

        $str=array(
            "fields" => "pd.product_name,pd.product_spec,pd.brand_mark,pcl.*,ROUND((pcl.highs+pcl.low)/2,2) as avg_price",
            "where" => "pcl.product_id = pd.product_id and pd.disable=0 and pd.`language`='cn'
                        and pcl.`disable`=0 and pcl.product_id in($str_pid) and pcl.renew_date<'".$now_date."'",
            "order" => "pd.listorder"
        );

        $products=$db->select($table_name,$str);

        return $products;

    }


    //获取主产品中价格当日已更新的产品价格详情
    static function mainPriceIsUpdate($pid_array){

        //获取当前日期
        $now_date=date('Y-m-d');

        global $conf;

        $db=new MyAdodb();

        //处理产品编号
        $str_pid=implode(",",$pid_array);

        $table_name=$conf["DB_PREFIX"]."price_product_latest as pcl ,".$conf["DB_PREFIX"]."product_detail as pd";

        $str=array(
            "fields" => "pd.product_name,pd.product_spec,pd.brand_mark,pcl.*,ROUND((pcl.highs+pcl.low)/2,2) as avg_price",
            "where" => "pcl.product_id = pd.product_id and pd.disable=0 and pd.`language`='cn'
                        and pcl.`disable`=0 and pcl.product_id in($str_pid) and pcl.renew_date='".$now_date."' ",
            "order" => "pd.listorder"
        );

        $products=$db->select($table_name,$str);

        return $products;
    }


    //获取指定产品类型上海期货实时价格
    static function shfeRealTimePrice($type_array){

        global $conf;

        $db=new MyAdodb();

        //切换到smmRTDB 数据库
        $db->changeDb($conf["DB_connect_url"],$conf["DB_connect_root"],$conf["DB_connect_password"],"smmRTDB");

        //处理类型key数组，转换为字符串
        $str_type="'".implode("','",array_keys($type_array))."'";

        $table_name=$conf["DB_PREFIX"]."shfe_min as a, (
                        select type,MAX(add_time) as add_time from ".$conf["DB_PREFIX"]."shfe_min where type in($str_type)
                        and is_agr=1 group by type
                    ) as b";

        $str=array(
            "fields" => "a.type,a.symbol,a.`name`,a.trade,a.`change`",
            "where"  => "a.type=b.type and a.add_time=b.add_time and a.is_agr=1",
        );

        $result=$db->select($table_name,$str);

        $data=array();

        //如果结果不为空，处理类型名称
        foreach($result as $key=>$value){

            $data[$key]=$value;

            $type=$value["type"];

            $type_name=$type_array[$type];

            if(!empty($type_name)) $data[$key]["type_name"]=$type_name;
        }

        return $data;
    }


    //获取一组产品评伦敦期货最新价格
    static function lmePriceByArray($product_id_array){

        global $conf;

        $db=new MyAdodb();

        $str_pid=implode(",",$product_id_array);

        $table_name=$conf["DB_PREFIX"]."price_futurelme as a ,(
                    select product_id ,MAX(renew_date) as renew_date from ".$conf["DB_PREFIX"]."price_futurelme
                    where product_id in ($str_pid)
                    group by product_id
                    ) as b ,".$conf["DB_PREFIX"]."product_detail as c";

        $str=array(
            "fields" => "c.product_name,a.product_id,a.opening,a.highs,a.low,a.closed,a.spot_price,a.settlement_price,a.renew_date ",
            "where" => "a.product_id = b.product_id and a.renew_date = b.renew_date and a.product_id=c.product_id and a.`disable`=0 and c.`language`='cn'",
        );

        $result=$db->select($table_name,$str);

        return $result;

    }


    //获取一组产品上海期货最新价格
    static function shfePriceByArray($product_id_array){

        global $conf;

        //处理产品编号
        $str_pid=implode(",",$product_id_array);

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_futureshfe as a ,(
                    select product_id ,MAX(renew_date) as renew_date from ".$conf["DB_PREFIX"]."price_futureshfe
                    where product_id in ($str_pid)
                    group by product_id
                    ) as b ,".$conf["DB_PREFIX"]."product_detail as c";

        $str=array(
                "fields" => "c.product_name,a.product_id,a.opening,a.am_closed,a.highs,a.low,a.closed,a.spot_price,a.settlement_price,a.first_settlement,a.settlement_price_last,a.renew_date ",
                "where" => "a.product_id = b.product_id and a.renew_date = b.renew_date and a.product_id=c.product_id and a.`disable`=0 and c.`language`='cn'",
            );

        $result=$db->select($table_name,$str);

        return $result;
    }


    //获取指定产品伦敦期货最新价格
    static function lmePriceById($id){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_futurelme";

        $str=array(
            "fields" => "product_id,opening,closed,renew_date",
            "where" => "product_id=$id and disable=0 ",
            "order" => "renew_date desc",
            "count" => 1
        );

        $result=$db->select($table_name,$str);

        if(empty($result)) return null;

        return $result[0];

    }


    //获得指定产品上海期货最新价格
    static function shfePriceById($id){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_futureshfe";

        $str=array(
            "fields" => "product_id,opening,closed,renew_date",
            "where" => "product_id=$id and disable=0 ",
            "order" => "renew_date desc",
            "count" => 1
        );

        $result=$db->select($table_name,$str);

        if(empty($result)) return null;

        return $result[0];
    }


    //查询指定产品SMM价格历史,指定时间区间
    static function priceHistory($id,$begin_date,$end_date){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_product";

        $str=array(
            "fields" => "price_id,highs,low,ROUND((highs+low)/2,2) as avg_price,price_state,renew_date",
            "where" => "product_id=$id and disable=0 and renew_date between '$begin_date'  and '$end_date'",
            "order" => "renew_date"
        );

        $products=$db->select($table_name,$str);

        if(empty($products)) return null;

        return $products;

    }


    //查询指定产品上期所(shfe)历史价格,指定时间区间
    static function priceShfeHistory($id,$begin_date,$end_date){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_futureshfe";

        $str=array(
            "fields" => "price_id,opening,closed,am_closed,highs,low,settlement_price,renew_date ",
            "where" => "product_id=$id and disable=0 and renew_date between '$begin_date'  and '$end_date'",
            "order" => "renew_date"
        );

        $price=$db->select($table_name,$str);

        if(empty($price)) return null;

        return $price;


    }


    //查询指定产品伦敦期货(lme)历史价格,指定时间区间
    static function priceLmeHistory($id,$begin_date,$end_date){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_futurelme";

        $str=array(
            "fields" => "opening,closed,am_closed,highs,low,spot_price,settlement_price,renew_date",
            "where" => "product_id=$id and disable=0 and renew_date between '$begin_date'  and '$end_date'",
            "order" => "renew_date"
        );

        $price=$db->select($table_name,$str);

        if(empty($price)) return null;

        return $price;


    }


    //查询产品价格详情
    static function lastPriceInfo($id){

        global $conf;

        $db=new MyAdodb();

        $table_name=$conf["DB_PREFIX"]."price_product_latest as pcl ,".$conf["DB_PREFIX"]."product_detail as pd";

        $str=array(
            "fields" => "pd.product_name,pd.product_spec,pd.brand_mark,pcl.*,ROUND((pcl.highs+pcl.low)/2,2) as avg_price",
            "where" => "pcl.product_id = pd.product_id and pd.disable=0 and pd.`language`='cn' and pcl.`disable`=0 and pcl.product_id=$id",
        );

        $products=$db->select($table_name,$str);

        if(empty($products)) return null;

        return $products[0];

    }


    //按产品编号数组，查询查询产品详情
    static function lastPriceInfoByArray($ids_array){

        global $conf;

        $db=new MyAdodb();

        //处理产品编号
        $str_pid=implode(",",$ids_array);

        $table_name=$conf["DB_PREFIX"]."price_product_latest as pcl ,".$conf["DB_PREFIX"]."product_detail as pd";

        $str=array(
            "fields" => "pd.product_name,pd.product_spec,pd.brand_mark,pcl.*,ROUND((pcl.highs+pcl.low)/2,2) as avg_price",
            "where" => "pcl.product_id = pd.product_id and pd.disable=0 and pd.`language`='cn' and pcl.`disable`=0 and pcl.product_id in($str_pid)",
            "order" => "pd.listorder"
        );

        $products=$db->select($table_name,$str);

        return $products;

    }




}